package src.jdk8;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020/5/26
 */
public class StreamDemo {

    public static void main(String[] args) {

        StreamDemo streamDemo = new StreamDemo();
        streamDemo.baseTest();

    }

    /**
     * Stream的一些基本功能
     */
    private void baseTest() {

        List<Integer> list = Stream.of(1, 3, 4, 6, 2)
                // 过滤
                .filter(x -> x > 3)
                // 限制
                .limit(2)
                // 操作
                .map(y -> y + 6)
                // 排序
                .sorted((o1, o2) -> o2 - o1)
                // 转换成list
                .collect(Collectors.toList());
        System.out.println(list);


        List<Integer> list1 = Arrays.asList(3, 5, 6, 8, 1);

        // 匹配 只要其中任意一个元素大于0 则返回true
        boolean isAny = list1.stream().anyMatch(i -> i > 0);
        // 所有元素都要满足
        boolean isAll = list1.stream().allMatch(x -> x > 3);
        // 没有元素满足返回true
        boolean isNone = list1.stream().noneMatch(i -> i < 0);
        System.out.println("isAny：" + isAny);
        System.out.println("isAll：" + isAll);
        System.out.println("isNone：" + isNone);

        Optional<Integer> min = list1.stream().min(Comparator.naturalOrder());
        System.out.println(min.get());

        Optional<Integer> reduce = list1.stream().reduce(Integer::sum);
        System.out.println(reduce.get());

        Set<Integer> collect = Stream.iterate(0, integer -> integer + 3)
                .limit(10)
                .collect(Collectors.toCollection(LinkedHashSet::new));
        System.out.println(collect);

    }

}
